#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include "mynteyed/camera.h"
#include "mynteyed/utils.h"
#include <string>
#include <time.h>
#include <stdlib.h>

using namespace std;

MYNTEYE_USE_NAMESPACE

string getTime()
{
	time_t timep;
	time(&timep); //获取time_t类型的当前时间
	char tmp[64];
	strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&timep));//对日期和时间进行格式化
	return tmp;
}
int getDepth(int x, int y) {
	int depthvaue = 0;
	string   time = getTime();
	cout << time << endl;
	Camera cam;
	DeviceInfo dev_info;
	if (!util::select(cam, &dev_info)) {
		return 1;
	}
	util::print_stream_infos(cam, dev_info.index);
	std::cout << "Open device: " << dev_info.index << ", "
		<< dev_info.name << std::endl << std::endl;
	OpenParams params(dev_info.index);
	{
		// Framerate: 10(default usb3.0) 5(default usb2.0), [0,60], [30](STREAM_2560x720)
		params.framerate = 10;
		params.stream_mode = StreamMode::STREAM_1280x720;  // hd
		params.ir_intensity = 4;
		// Colour depth image, default 5000. [0, 16384]
		params.colour_depth_value = 5000;
	}
	cam.EnableImageInfo(true);
	cam.Open(params);
	std::cout << std::endl;
	if (!cam.IsOpened()) {
		std::cerr << "Error: Open camera failed" << std::endl;
		return 1;
	}
	std::cout << "Open device success" << std::endl << std::endl;
	bool is_depth_ok = cam.IsStreamDataEnabled(ImageType::IMAGE_DEPTH);
	time = getTime();
	cout << time << endl;
	for (;;) {
		cam.WaitForStream();
		if (is_depth_ok) {
			auto image_depth = cam.GetStreamData(ImageType::IMAGE_DEPTH);
			if (image_depth.img) {
				time = getTime();
				cout << endl << time << endl;
				cv::Mat depth;
				depth = image_depth.img->ToMat();
				int count = 0;
				for (int i = x;i < (x + 2);i++)
				{
					for (int j = y;j < (y + 2);j++)
					{
						ushort Z = depth.at<ushort>(i, j);
						if (Z > 0)
						{
							count++;
							depthvaue += Z;
						}
						std::cout << i << "-" << j << ":length=" << Z << std::endl;
					}
				}
				if (count > 0)
				{
					depthvaue = depthvaue / count;
				}
				break;
			}
		}
	}
	cam.Close();
	return depthvaue;
}